package com.ai.demo.api;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.qianfan.QianFanChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Stream;


/**
 * 聊天接口
 */
@RestController
@RequestMapping("/chat")
public class ChatApi {

    @Resource
    private QianFanChatModel chatClient;
    private final String accessKey = "fPBzv0lbRVlsaPfHsPjHKixY";
    private final String secretKey = "0jvguEhHDHUadidESY1I1iW00j0W5cqE";

    /*@GetMapping("/ai/generate")
    public String chatAI(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message){
        Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH,accessKey, secretKey);
        ChatResponse chatResponse = qianfan.chatCompletion().model("ERNIE-4.0-8K")
                .addMessage("user", message) // 聊天消息内容，以用户的角色发送
                .temperature(0.7)
                .topP(0.9)
                .execute();
        return chatResponse.getResult();
    }*/

    /*@GetMapping("/ai/stream")
    public void chatStreamAI(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message, HttpServletResponse servletResponse) throws IOException {
        PrintWriter writer = servletResponse.getWriter();
        try(StreamIterator<CompletionResponse> response = new Qianfan(Auth.TYPE_OAUTH, accessKey, secretKey)
                .completion().model("CodeLlama-7b-Instruct").prompt(message)
                .executeStream()
        ){
            while (response.hasNext()){
                String result = response.next().getResult();
                System.out.println(result);
                System.out.println("==================");
                writer.write(result);
            }
        }
    }*/

    @GetMapping("spring/ai/generate")
    public String springChatAI(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message){
        Prompt prompt = new Prompt(message);
        ChatResponse response = chatClient.call(prompt);
        Generation result = response.getResult();
        String content = result.getOutput().getContent();
        return content;
    }

    @GetMapping("spring/ai/stream")
    public void chatStreamAI(@RequestParam(value = "message", defaultValue = "请给我讲一个笑话") String message, HttpServletResponse servletResponse) throws IOException {
        // 设置响应的内容类型并指定字符编码
        servletResponse.setContentType("text/html;charset=UTF-8");
        // 显式设置字符编码
        servletResponse.setCharacterEncoding("UTF-8");
        PrintWriter writer = servletResponse.getWriter();
        Prompt prompt = new Prompt(message);
        Stream<ChatResponse> chatResponseStream = chatClient.stream(prompt).toStream();
        chatResponseStream.forEach(chatResponse -> {
            Generation result = chatResponse.getResult();
            String content = result.getOutput().getContent();
            System.out.println(content);
            System.out.println("================");
            writer.write(content);
        });
    }

    @GetMapping("/hb")
    public String hb(){
        return "ok";
    }

}
